from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time,requests,json,base64


driver = webdriver.Chrome(executable_path=r'C:\Users\hello\Desktop\selenium\chromedriver.exe')
driver.maximize_window()
# driver.set_window_size(3000, 1500)

driver.get('https://kyfw.12306.cn/otn/resources/login.html')

time.sleep(5)
driver.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a').click()
 
time.sleep(5)
driver.find_element_by_id("J-userName").send_keys(u"15717691052")  
driver.find_element_by_id("J-password").send_keys(u"15717691052")


img = driver.find_element_by_xpath('//*[@id="J-loginImg"]').get_attribute("src").split(',')[1]  
# print(img)
imagedata = base64.b64decode(img)
with open('12306.jpg','wb') as f:
    f.write(imagedata)

time.sleep(5)

# ActionChains(driver).move_by_offset(1300, 360).click().perform()

# from PIL import ImageGrab
# im = ImageGrab.grab()
# im.save('12306.png')


zuobiao = {
    '1':[1260,360],
    '2':[1343,353],
    '3':[1400,353],
    '4':[1450,353],

    '5':[1240,430],
    '6':[1323,430],
    '7':[1390,430],
    '8':[1460,430]
}

# msg = input('请输入图片顺序,以逗号分隔：')


# msg = msg.split(',')
# # print(msg)

import requests
from bs4 import BeautifulSoup

res = requests.post('http://littlebigluo.qicp.net:47720',files={'pic_xxfile': open('12306.jpg', 'rb')})
# print(res.text)

soup = BeautifulSoup(res.text)
msg = str(soup.select('B')[0].string).split(' ')

for i in range(len(msg)):
    ActionChains(driver).move_by_offset(zuobiao[msg[i]][0],zuobiao[msg[i]][1]).click().perform()
    ActionChains(driver).move_by_offset(-zuobiao[msg[i]][0], -zuobiao[msg[i]][1]).perform()


driver.find_element_by_id("J-login").click()
